package com.weishuang.monitor.leader.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.weishuang.monitor.leader.entity.SysRoleMenu;
import com.weishuang.monitor.leader.mapper.SysRoleMenuMapper;
import com.weishuang.monitor.leader.service.ISysRoleMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 角色菜单关联表 服务实现类
 * </p>
 *
 * @author weishuang
 * @since 2021-03-23
 */
@Service
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuMapper, SysRoleMenu> implements ISysRoleMenuService {

    /**
     * 授权菜单和权限给角色
     * @param roleId
     * @param menuIds
     * @return
     */
    @Override
    public void authorizationMenuToRoleAndPermissions(String roleId, String[] menuIds) {
        LambdaQueryWrapper<SysRoleMenu> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(SysRoleMenu::getRoleId, roleId);
        baseMapper.delete(queryWrapper);
        if (menuIds != null && menuIds.length > 0) {
            List<SysRoleMenu> sysRoleMenus = new ArrayList<>();
            for (String menuId : menuIds) {
                SysRoleMenu sysRoleMenu = new SysRoleMenu();
                sysRoleMenu.setMenuId(menuId);
                sysRoleMenu.setRoleId(roleId);
                sysRoleMenus.add(sysRoleMenu);
            }
            this.saveBatch(sysRoleMenus);
        }
    }
}
